//
//  WalletViewController.m
//  MyNewProject
//
//  Created by 王春辉 on 2020/1/1.
//  Copyright © 2020年 sun. All rights reserved.
//

#import "WalletViewController.h"
#import "WalletDetailViewController.h"
#import "XMRechargeViewController.h"
#import "DrawCashViewController.h"

#pragma mark -- mark 数据相关
#import "UserInfoModel.h"

@interface WalletViewController () {
    UIButton *_mybalanceBtn;
    UIButton *_bailBtn;
    
    UILabel *_balanceTipLabel;
    UILabel *_ableBalanceTipLabel;
    UILabel *_balanceLabel;
    UILabel *_abelBalanceLabel;
    
    UIView *_leftLineView;
    UIView *_rightLineView;
    
    UserInfoDataModel *_model;
}

@end

@implementation WalletViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navLineHidden = false;
    self.gk_navTitle = @"我的钱包";
    self.view.backgroundColor = SKColorWithHex(0xF1F1F1);
    [self initNavigationBar];
    [self initContentViews];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestData];
}
- (void)initNavigationBar {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"明细" forState:UIControlStateNormal];
    [btn setTitleColor:SKColorWithHex(0x333333) forState:UIControlStateNormal];
    btn.titleLabel.font = SKFONT_SIZE(14);
    [btn addTarget:self action:@selector(barButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.gk_navRightBarButtonItem = item;
}

- (void)initContentViews {
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn1 setTitle:@"我的余额" forState:UIControlStateNormal];
    [btn1 setTitleColor:SKColorWithHex(0x999999) forState:UIControlStateNormal];
    [btn1 setTitleColor:SKColorWithHex(0x000000) forState:UIControlStateSelected];
    btn1.titleLabel.font = SKFONT_Medium(15);
    btn1.backgroundColor = [UIColor whiteColor];
    [btn1 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setTitle:@"保证金" forState:UIControlStateNormal];
    [btn2 setTitleColor:SKColorWithHex(0x999999) forState:UIControlStateNormal];
    [btn2 setTitleColor:SKColorWithHex(0x000000) forState:UIControlStateSelected];
    btn2.titleLabel.font = SKFONT_Medium(15);
    btn2.backgroundColor = [UIColor whiteColor];
    [btn2 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    
    UILabel *label1 = [[UILabel alloc] init];
    label1.textColor = SKColorWithHex(0x444444);
    label1.font = SKFONT_SIZE(12);
    label1.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:label1];
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.textColor = SKColorWithHex(0xFF2424);
    label2.font = SKFONT_SIZE(12);
    label2.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:label2];
    
    UILabel *label1_1 = [[UILabel alloc] init];
    label1_1.textColor = SKColorWithHex(0x999999);
    label1_1.font = SKFONT_SIZE(14);
    label1_1.textAlignment = NSTextAlignmentCenter;
    label1_1.text = @"余额（元）";
    [bgView addSubview:label1_1];
    
    UILabel *label2_2 = [[UILabel alloc] init];
    label2_2.textColor = SKColorWithHex(0x999999);
    label2_2.font = SKFONT_SIZE(14);
    label2_2.textAlignment = NSTextAlignmentCenter;
    label2_2.text = @"可用余额（元）";
    [bgView addSubview:label2_2];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = SKColorWithHex(0xF1F1F1);
    [bgView addSubview:lineView];
    
    
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.top.mas_offset(SKTopBar_Height);
        make.size.mas_equalTo(btn2);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(46.0/667);
    }];
    
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(btn1.mas_right);
        make.right.mas_offset(0);
        make.top.mas_equalTo(btn1);
    }];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_equalTo(btn1.mas_bottom).mas_offset(10);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(94.0/667);
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_offset(0);
        make.width.mas_equalTo(0.5);
        make.top.mas_offset(20);
    }];
    
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.right.mas_equalTo(lineView.mas_left).mas_offset(-10);
        make.bottom.mas_equalTo(bgView.mas_centerY);
    }];
    
    [label1_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(label1);
        make.top.mas_equalTo(label1.mas_bottom);
    }];
    
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(lineView.mas_right).mas_offset(10);
        make.right.mas_offset(-10);
        make.bottom.mas_equalTo(label1);
    }];
    
    [label2_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(label2);
        make.top.mas_equalTo(label2.mas_bottom);
    }];
    
    _mybalanceBtn = btn1;
    _bailBtn = btn2;
    
    _balanceTipLabel = label1_1;
    _ableBalanceTipLabel = label2_2;
    _balanceLabel = label1;
    _abelBalanceLabel = label2;
    
    
    UIButton *drawCashBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [drawCashBtn setTitle:@"提 现" forState:UIControlStateNormal];
    [drawCashBtn setTitleColor:SKColorWithHex(0x444444) forState:UIControlStateNormal];
    drawCashBtn.titleLabel.font = SKFONT_BOLD_SIZE(15);
    drawCashBtn.backgroundColor = [UIColor whiteColor];
    [drawCashBtn addTarget:self action:@selector(drawCashButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:drawCashBtn];
    
    UIButton *topUpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [topUpBtn setTitle:@"充 值" forState:UIControlStateNormal];
    [topUpBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    topUpBtn.titleLabel.font = SKFONT_BOLD_SIZE(15);
    topUpBtn.backgroundColor = SKColorWithHex(0xE33030);
    [topUpBtn addTarget:self action:@selector(topUpButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:topUpBtn];
    
    [drawCashBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.size.bottom.mas_equalTo(topUpBtn);
        make.height.mas_equalTo(self.view).multipliedBy(49.0/667);
        make.bottom.mas_offset(-SKBottomSafe_Height);
    }];
    
    [topUpBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(drawCashBtn.mas_right);
        make.right.mas_offset(0);
    }];
    
    UIView *leftLineView = [[UIView alloc] init];
    leftLineView.backgroundColor = SKColorWithHex(0xE33030);
    leftLineView.layer.cornerRadius = 1.0;
    [self.view addSubview:leftLineView];
    
    UIView *rightLineView = [[UIView alloc] init];
    rightLineView.backgroundColor = SKColorWithHex(0xE33030);
    rightLineView.layer.cornerRadius = 1.0;
    [self.view addSubview:rightLineView];
    
    [leftLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.centerX.mas_equalTo(btn1);
        make.size.mas_equalTo(CGSizeMake(44, 3));
    }];
    
    [rightLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.bottom.mas_equalTo(leftLineView);
        make.centerX.mas_equalTo(btn2);
    }];
    _leftLineView = leftLineView;
    _rightLineView = rightLineView;
    
    rightLineView.hidden = true;
    label1.text = @" ";
    label2.text = @" ";
    _mybalanceBtn.selected = true;
}


#pragma mark -- mark EVENT

- (void)buttonClick:(UIButton *)sender {
    sender.selected = true;
    if (sender == _mybalanceBtn) {
        // 我的余额
        _leftLineView.hidden = false;
        _rightLineView.hidden = true;
        _balanceTipLabel.text = @"余额（元）";
        _ableBalanceTipLabel.text = @"可用余额（元）";
        _bailBtn.selected = false;
        _balanceLabel.text = _model.yue;
        _abelBalanceLabel.text = _model.keyong_yue;
    }else {
        // 保证金
        _leftLineView.hidden = true;
        _rightLineView.hidden = false;
        _balanceTipLabel.text = @"保证金（元）";
        _ableBalanceTipLabel.text = @"可用保证金（元）";
        _mybalanceBtn.selected = false;
        _balanceLabel.text = _model.bail;
        _abelBalanceLabel.text = _model.keyong_bail;
    }
}

/**
 体现

 @param sender sender
 */
- (void)drawCashButtonClick:(UIButton *)sender {
    DrawCashViewController *vc = [[DrawCashViewController alloc] init];
    [self.navigationController pushViewController:vc animated:true];
}

/**
 充值

 @param sender sender
 */
- (void)topUpButtonClick:(UIButton *)sender {
    XMRechargeViewController *vc = [[XMRechargeViewController alloc] init];
    SKWeakSelf;
    vc.paySuccessResultBlock = ^{
      // 刷新数据
        [weakSelf requestData];
    };
    [self.navigationController pushViewController:vc animated:true];
}

/**
 明细

 @param sender sender
 */
- (void)barButtonClick:(UIButton *)sender {
    WalletDetailViewController *vc = [[WalletDetailViewController alloc] init];
    [self.navigationController pushViewController:vc animated:true];
}

#pragma mark -- mark 获取网络数据
- (void)requestData {
    SKWeakSelf;
    NSDictionary *param = @{@"uid":self.uid,
                            @"loginKey":self.loginKey
    };
    [YQNetworking getWithUrl:@"my/getUserInfo" params:param reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        UserInfoModel *model = [UserInfoModel modelWithDictionary:response];
        if ([model isSuccess]) {
            [weakSelf refreshUerinfoWith:model.user_info];
            YPLoginUserInfo *info = [YPLoginUserInfo modelObjectWithDictionary: model.user_info.modelToJSONObject];
            [UserInfoSingleton shareInstance].userInfo = info;
            SKUserUserDefaults_Save(response[@"user_info"] , @"UserInfo");
        }else {
            [weakSelf showMessage:model.msg];
        }
    } failBlock:^(NSError *error) {
        [weakSelf debugShowMessage:error.localizedDescription];
    }];
}


#pragma mark -- mark 业务逻辑
- (void)refreshUerinfoWith:(UserInfoDataModel *)model {
    _model = model;
    if (_bailBtn.isSelected) {
        // 保证金
        _balanceLabel.text = model.bail;
        _abelBalanceLabel.text = model.keyong_bail;
    }else if(_mybalanceBtn.isSelected) {
        // 余额
        _balanceLabel.text = model.yue;
        _abelBalanceLabel.text = model.keyong_yue;
    }else {}
    
}
@end
